<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2018/12/27
 * Time: 9:02
 */

namespace app\sports_admin\model;


use think\Config;
use think\Model;
use think\Request;

class ParentsChildren extends Model
{
    protected $table = "parents_children";
    protected $pk    = "id";
    public function getBirthdayAttr($values)
    {
        return date("Y-m-d",$values);
    }

    //展示子女信息
    public static function getChildrenList($page,$rows,$sort,$sortOrder,$searchName,$parentId)
    {
        $result = self::where(function($query)use($searchName){
            if($searchName){
                $query->where('name','like',"%{$searchName}%");
            }
        })->where('parent_id',$parentId)->order("{$sort}","{$sortOrder}")->page($page)->limit($rows)->select();

        $count = self::where(function($query)use($searchName){
            if($searchName){
                $query->where('name','like',"%{$searchName}%");
            }
        })->where('parent_id',$parentId)->count();
        return json(['rows'=>$result,'total'=>$count]);
    }
    //修改子女信息
    public static function childrensUpdate()
    {
        $data = Request::instance()->only("id,name,equipment_id,birthday,img,sex,height,weight,blood_pressure,congenital_history,hobby_sports,sos_phone,stu_num");
        $update_time = time();
        return $is_update = self::where('id',$data['id'])->update([
            "name"=>$data['name'],"equipment_id"=>$data['equipment_id'],"birthday"=>$data['birthday'],"img"=>$data['img'],
            "sex"=>$data['img'],"height"=>$data['height'],"blood_pressure"=>$data['blood_pressure'],"congenital_history"=>$data['congenital_history'],
            "hobby_sports"=>$data['hobby_sports'],"sos_phone"=>$data['sos_phone'],"stu_num"=>$data['stu_num'],
            "update_time"=>$update_time
        ]);
    }
    //删除子女信息
    public static function childrensAdd()
    {
        $data = Request::instance()->only("parent_id,name,equipment_id,birthday,img,sex,height,weight,blood_pressure,congenital_history,hobby_sports,sos_phone,stu_num");
        return   ParentsChildren::create($data);
    }


}